package org.springboot.base.demo.controller;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springboot.base.demo.service.RedisService;
import org.springboot.base.user.does.UserDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ZJL
 * @DateTime 2019年2月18日 下午2:58:31
 * Redis 相关的Controller
 **/
@RestController
@RequestMapping("/redis")
public class RedisController {
	@Autowired
	private RedisTemplate<String, Object> redisTemplate;
	
	@Autowired
	private RedisService redisService;

	@RequestMapping("/doString")
	public String doString() {
		this.redisTemplate.opsForValue().set("method","doString");
		Object obj = this.redisTemplate.opsForValue().get("method");
		System.out.println("method:"+obj);
		//this.redisService.getUser(1l);
		this.redisService.getUser("userName");
		return obj.toString();
	}
	
	@RequestMapping("/doList")
	public Object doList() {
		List<String> list = new ArrayList<String>();
		list.add("list10001");
		list.add("list10002");
		list.add("list10003");
		list.add("list10004");
		list.add("list10005");
		
		this.redisTemplate.opsForList().rightPushAll("list", list);
		List<Object> list1 = this.redisTemplate.opsForList().range("list", 0, -1);
		for(Object obj:list1) {
			System.out.println("List:Object:"+obj.toString());
		}
		
		return list1;
	}
	
	@RequestMapping("/doSetSession")
	public Object doSetSession(HttpServletRequest request,String userName) {
		UserDO user = new UserDO();
		user.setUserId(10003l);
		user.setUserName(userName);
		user.setUserAge(10003);
		user.setUserBirthday(Calendar.getInstance().getTime());
		user.setUserPwd("userPwd10003");
		//获取session对象
		HttpSession session = request.getSession();
		session.setAttribute("userName", userName);
		session.setAttribute("user-session", user);
		return session;
	}
	
	@RequestMapping("/doGetSession")
	public Object doGetSession(HttpServletRequest request) {
		HttpSession session = request.getSession();
		session.getAttribute("userName");
		UserDO user = (UserDO)session.getAttribute("user-session");
		return user;
	}
	
	@RequestMapping("/getUser")
	@Cacheable(value="user-get",key="'userId:'+#userId")
	public UserDO getUser(Long userId) {
		UserDO user = this.redisService.getUser(userId);
		System.out.println("若下面没出现“无缓存的时候调用”字样且能打印出数据表示测试成功"); 
		return user;
	}
	
	@RequestMapping("/listUser")
	@Cacheable(value="user-list",key="'listUser'")
	public List<UserDO> listUser() {
		List<UserDO> list = this.redisService.listUser();
		System.out.println("若下面没出现“无缓存的时候调用”字样且能打印出数据表示测试成功"); 
		return list;
	}
}
